package com.yoyo.framework.manager.factory;

import com.yoyo.common.constant.Constants;
import com.yoyo.common.utils.AddressUtils;
import com.yoyo.common.utils.LogUtils;
import com.yoyo.common.utils.ServletUtils;
import com.yoyo.common.utils.ShiroUtils;
import com.yoyo.common.utils.StringUtils;
import com.yoyo.common.utils.spring.SpringUtils;
import com.yoyo.framework.shiro.session.OnlineSession;
import com.yoyo.system.domain.SysLoginInfo;
import com.yoyo.system.domain.SysUserOnline;
import com.yoyo.system.service.ISysUserOnlineService;
import com.yoyo.system.service.impl.SysLoginInfoServiceImpl;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

/**
 * @author zyl
 * @Date 2021/6/27 -14:58
 */
@Slf4j
public class AsyncFactory {
    /**
     *
     * @return
     */
    public static TimerTask recordLoginInfo(final String username,final String status ,final String message,final Object... args ){
     final    UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        String ip = ShiroUtils.getIp();

        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder sb = new StringBuilder();
                sb.append(LogUtils.getBlock(ip));
                sb.append(address);
                sb.append(LogUtils.getBlock(username));
                sb.append(LogUtils.getBlock(status));
                sb.append(LogUtils.getBlock(message));
                //打印信息到日志
                log.info(sb.toString(),args);
                // 获取操作系统
                String os = userAgent.getOperatingSystem().getName();

                //获取浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装登录信息对象
                SysLoginInfo sysLoginInfo=new SysLoginInfo();
                sysLoginInfo.setLoginName(username);
                sysLoginInfo.setIpaddr(ip);
                sysLoginInfo.setBrowser(browser);
                sysLoginInfo.setOs(os);
                sysLoginInfo.setMsg(message);
                if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
                {
                    sysLoginInfo.setStatus(Constants.SUCCESS);
                }
                else if (Constants.LOGIN_FAIL.equals(status))
                {
                    sysLoginInfo.setStatus(Constants.FAIL);
                }
                SpringUtils.getBean(SysLoginInfoServiceImpl.class).insertLoginInfo(sysLoginInfo);
            }
        };
    }

    /**
     * 同步session 到数据库
     * @param onlineSession
     * @return
     */
    public static TimerTask syncSessionToDB(OnlineSession onlineSession) {
        return new TimerTask() {
            @Override
            public void run() {
                SysUserOnline online = new SysUserOnline();
                online.setSessionId(String.valueOf(onlineSession.getId()));
                online.setDeptName(onlineSession.getDeptName());
                online.setLoginName(onlineSession.getLoginName());
                online.setStartTimestamp(onlineSession.getStartTimestamp());
                online.setLastAccessTime(onlineSession.getLastAccessTime());
                online.setExpireTime(onlineSession.getTimeout());
                online.setIpaddr(onlineSession.getHost());
                online.setLoginLocation(AddressUtils.getRealAddressByIP(onlineSession.getHost()));
                online.setBrowser(onlineSession.getBrowser());
                online.setOs(onlineSession.getOs());
                online.setStatus(onlineSession.getStatus());
                SpringUtils.getBean(ISysUserOnlineService.class).saveOnline(online);
            }
        };
    }
}
